﻿using AutoMapper;
using MediatR;
using ProductManagement.Applcation.Command;
using ProductManagement.Domain.ProductsDomain;
using ProductManagement.Infrstructre.Interface;

namespace ProductManagement.Applcation.CommandHanlers
{
    public class ProductsTypeUpdateCommandHandlers : IRequestHandler<ProductsTypeUpdateCommand, int>
    {
        protected readonly InterfaceProductsTypeRepository repository;
        protected readonly IMapper mediator;

        public ProductsTypeUpdateCommandHandlers(InterfaceProductsTypeRepository repository, IMapper mediator)
        {
            this.repository = repository;
            this.mediator = mediator;
        }

        public async Task<int> Handle(ProductsTypeUpdateCommand request, CancellationToken cancellationToken)
        {
            var list = (await repository.GetAll()).Where(x => x.PTClass == request.dtos.PTClass);
            if (list == null)
            {
                return -1;
            }
            else
            { 
                return await repository.UpdateAsync(mediator.Map<ProductType>(request.dtos));
            }
        }
    }
}
